﻿@using Smartstore.Web.Models.Catalog

@model IList<BrandModel>

@{
    Assets.AppendTitleParts(T("PageTitle.Manufacturers"));

    var numericList = Model.Where(x => Char.IsNumber(x.Name.Value[0])).OrderBy(x => x.Name).ToArray();
    var alphabeticalList = Model.Except(numericList).Where(x => x.Name.HasValue()).OrderBy(x => x.Name).ToArray();
    var sortManufacturersAlphabetically = (bool)ViewBag.SortManufacturersAlphabetically;

    async Task RenderManufacturer(BrandModel item)
    {
        var url = Url.RouteUrl("Manufacturer", new { SeName = item.SeName });

        <article class="art" data-id="@item.Id">
            @if (item.Image != null && item.Image.HasImage())
            {
                var image = item.Image;
                <div class="art-picture-block">
                    <a href="@url" class="art-picture img-center-container">
                        <img sm-model="image" loading="lazy" />
                    </a>
                </div>
            }
            <div class="art-genericname">
                <a href="@url" title="@item.Name">
                    <span sm-language-attributes-for="item.Name">@item.Name</span>
                </a>
            </div>
        </article>
    }
}

<div class="page manufacturer-list-page">
    <div class="page-title">
        <h1 class="h2">@T("Manufacturers.List")</h1>
    </div>
    <div class="page-body">
        <zone name="manufacturers_all_top" />

        @if (sortManufacturersAlphabetically)
        {
            var groupedManus = alphabeticalList.GroupBy(x => char.ToUpper(x.Name.Value[0]));

            <div class="manufacturer-nav">
                <ul class="navbar justify-content-start list-unstyled">
                    @if (numericList.Any())
                    {
                        <li class="nav-item"><a class="nav-link" href="#number">@T("Catalog.ManufacturerAll.Numbers")</a></li>
                    }

                    @foreach (var manu in groupedManus)
                    {
                        <li class="nav-item"><a class="nav-link" href="#@manu.Key">@manu.Key</a></li>
                    }
                </ul>
            </div>

            if (numericList.Any())
            {
                <h4 id="number" class="manufacturer-list-title">@T("Catalog.ManufacturerAll.Numbers")</h4>
                <div class="artlist artlist-grid artlist-6-cols artlist-boxed manufacturer-grid">
                    @foreach (var item in numericList)
                    {
                        await RenderManufacturer(item);
                    }
                </div>
            }

            foreach (var manu in groupedManus)
            {
                <h4 id="@manu.Key" class="manufacturer-list-title">@manu.Key</h4>
                <div class="artlist artlist-grid artlist-6-cols artlist-boxed manufacturer-grid">
                    @foreach (var item in manu)
                    {
                        await RenderManufacturer(item);
                    }
                </div>
            }
        }
        else
        {
            <div class="artlist artlist-grid artlist-6-cols artlist-boxed manufacturer-grid">
                @foreach (var item in Model)
                {
                    await RenderManufacturer(item);
                }
            </div>
        }

        <zone name="manufacturers_all_bottom" />
    </div>
</div>

<script sm-target-zone="scripts" data-origin="manufacturer-all">
    $(function () {
        $('.manufacturer-nav .nav-link').click(function (e) {
            e.preventDefault();
            $.scrollTo(e.target.getAttribute('href'), 800);
            return false;
        });
    });
</script>